package com.new2i.action;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; 

import com.new2i.aop.LoginInterceptor;
import com.new2i.model.DoctorEntity;
import com.new2i.model.EmrNeonatusEntity;
import com.new2i.model.UserEntity;
import com.new2i.service.able.EmrService;
import com.new2i.util.Collection2JsonUtil; 

/**
 * 新生儿病历类别
 * 
 * @author 2i
 * @since 2015/08/19
 */
@Controller
@RequestMapping("api/emr")
public class EmrApiAction extends BaseAction {
  
	@Autowired
	private EmrService mEmrService; 
	 
	/**
	 * 获取七牛文件上传token
	 * 
	 * @author 2i
	 * @since 2015/08/19
	 * @param request
	 * @param response
	 * @param user
	 */
	@RequestMapping("/uptoken")
	public void getQiniuMchToken(HttpServletRequest request, HttpServletResponse response, UserEntity user) {  
		HashMap<String, Object> tokenInfo = mEmrService.getQiniuMchUptoken(); 
		String value = Collection2JsonUtil.byObject(tokenInfo);
		write2client(response, value); 
	}
	
	/**
	 * 创建用户新生儿病历
	 * @param request
	 * @param response
	 * @param doctor
	 * @param emr
	 * @param user
	 */
	@LoginInterceptor
	@RequestMapping("/create")
	public void createEmr(HttpServletRequest request, HttpServletResponse response, DoctorEntity doctor,
			EmrNeonatusEntity emr, UserEntity user) {
		emr.setDoctor_id(doctor.getDoctor_id());
		emr.setUser_id(user.getUser_id());
		HashMap<String, Object> tokenInfo = mEmrService.insertEmrNeonatus(emr); 
		String value = Collection2JsonUtil.byObject(tokenInfo);
		write2client(response, value); 
	}
	
	/**
	 * 修改用户新生儿病历
	 * @param request
	 * @param response
	 * @param doctor
	 * @param emr
	 * @param user
	 */
	@LoginInterceptor
	@RequestMapping("/update")
	public void updateEmr(HttpServletRequest request, HttpServletResponse response, DoctorEntity doctor,
			EmrNeonatusEntity emr, UserEntity user) {
		emr.setDoctor_id(doctor.getDoctor_id());
		emr.setUser_id(user.getUser_id());
		HashMap<String, Object> tokenInfo = mEmrService.updateEmrNeonatusById(emr); 
		String value = Collection2JsonUtil.byObject(tokenInfo);
		write2client(response, value); 
	}
	 
	/**
	 * 同步用户的病历图片
	 * 
	 * @param request
	 * @param response
	 * @param doctor
	 * @param emr
	 * @param user
	 */
	@LoginInterceptor 
	@RequestMapping("/item/sync")
	public void syncEmrNeonatusItem(HttpServletRequest request, HttpServletResponse response, DoctorEntity doctor,
			EmrNeonatusEntity emr, UserEntity user) {
		emr.setDoctor_id(doctor.getDoctor_id());
		emr.setUser_id(user.getUser_id());
		HashMap<String, Object> tokenInfo = mEmrService.syncEmrNeonatusItem(emr); 
		String value = Collection2JsonUtil.byObject(tokenInfo);
		write2client(response, value); 
	}
}
